<template >
  <div>
    <brand></brand>

    <el-card class="Hys_card">
      <!-- 点击添加用户弹出模态框 然后给这个按钮绑定个事件 将这个事件设置为ture  之后在data里定义数据对象
      更改组件里面的内容-->
      <el-button type="success" @click="dialogVisible = true">添加用户</el-button>
      <el-table :data="rolelist" border style="width: 100%">
        <el-table-column prop="id" label="商品id" width="140"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="remove(scope.row.id)"
            ></el-button>
            <el-button type="warning" icon="el-icon-star-off" size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 对话框 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="50%" @closed="reset1">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <div class="cont" v-for="(item,index) in props.row.children" :key="index">
                <!-- 左侧 -->
                <div class="left">
                  <el-button type="primary">
                    {{item.authName}}
                    <i class="el-icon-close el-icon--right" @click="del(item.id,props.row.id)"></i>
                  </el-button>
                </div>
                <!-- 右侧 -->
                <div class="right" v-for="(item1,index) in item.children" :key="index">
                  <div class="right_one">
                    <el-button type="success">
                      {{item1.authName}}
                      <i class="el-icon-close el-icon--right" @click="del(item.id,props.row.id)"></i>
                    </el-button>
                  </div>
                  <div class="right_two" v-for="(item2,index) in item1.children" :key="index">
                    <el-button type="warning">
                      {{item2.authName}}
                      <i class="el-icon-close el-icon--right" @click="del(item.id,props.row.id)"></i>
                    </el-button>
                  </div>
                </div>
              </div>
            </el-form>
          </template>
        </el-table-column>
        <el-form :model="rolesFrom" :rules="rules" ref="rolesFrom" label-width="100px">
          <el-form-item label="角色名称" prop="roleName">
            <el-input v-model="rolesFrom.roleName"></el-input>
          </el-form-item>
          <el-form-item label="角色地址" prop="roleDesc">
            <el-input v-model="rolesFrom.roleDesc"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import brand from "../Bread";
export default {
  components: {
    brand
  },
  data() {
    return {
      rolelist: [],
      dialogVisible: false,
      rolesFrom: {
        roleName: "",
        roleDesc: ""
      },
      rules: {
        roleName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        roleDesc: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.rolelis();
  },
  methods: {
    async rolelis() {
      let res = await this.$http.get("/roles");
      let { data, meta } = res.data;
      if (meta.status == 200) {
        this.rolelist = data;
      } else {
        return;
      }
    },
    async confirm() {
      this.$refs.rolesFrom.validate(async valid => {
        if (!valid) return false;
        let res = await this.$http.post("roles", this.rolesFrom);
        console.log(res);
        let { data, meta } = res.data;
        if (meta.status == 201) {
          this.rolesFrom = data;
          this.$message.success(meta.msg);
          this.rolelis();
          this.dialogVisible = false;
        } else {
          this.$message.error(meta.msg);
        }
      });
    },
    reset1() {
      this.$refs.rolesFrom.resetFields();
    }
  }
};
</script>
<style lang="scss" scoped>
</style>

